package com.dq.controller.op.opboss.org;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgPositionQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.org.centre.OrgDeptPositionCentre;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.utils.TreeBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Api(description = "岗位")
@RestController
@RequestMapping("sys/boss/orgPosition/")
public class OrgPositionController extends AllService {

    @ApiOperation("获取所有岗位， 或根据门店获取岗位")
    @PostMapping("/list")
    @RequiresPermissions("sys:orgPosition:list")
    public Vo list(@ApiParam("orgId: 门店ID， postName： 岗位名称") @RequestBody OrgPositionQuery orgPositionQuery) throws AdminException {
        orgPositionQuery.format();
        Map<String, Object> query = orgPositionQuery.parseMap();

        List<OrgPosition> orgPositionList;
        Long count;
        if(orgPositionQuery.getOrgId() == null) {
            if (StringUtils.isNotBlank(orgPositionQuery.getPostName())) {
                query.put("postNameLike", BaseAdminQuery.likeSql(orgPositionQuery.getPostName()));
                query.remove("postName");
            }
            if(orgPositionQuery.getBeginTime() != null){
                query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgPositionQuery.getBeginTime());
                query.remove("beginTime");
            }
            if(orgPositionQuery.getEndTime() != null){
                query.put("createTime"+ BaseDaoImpl.lteSqlStr,orgPositionQuery.getEndTime());
                query.remove("endTime");
            }
            orgPositionList = dao.find(dao.query2Hql(OrgPosition.class, query), query);
        }else {
            Org org = dao.get(Org.class, orgPositionQuery.getOrgId());
            if (org == null) {
                throw new AdminException("无对应门店");
            }
//            orgPositionQuery.setOrg(org);
            query.put("org_id",org.getId());
            query.remove("orgId");
            if (StringUtils.isNotBlank(orgPositionQuery.getPostName())) {
                query.put("orgPosition_postNameLike", BaseAdminQuery.likeSql(orgPositionQuery.getPostName()));
                query.remove("postName");
            }
            if(orgPositionQuery.getBeginTime() != null){
                query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgPositionQuery.getBeginTime());
                query.remove("beginTime");
            }
            if(orgPositionQuery .getEndTime()!= null){
                query.put("createTime"+ BaseDaoImpl.lteSqlStr,orgPositionQuery.getBeginTime());
                query.remove("endTime");
            }
            List<OrgDeptPositionCentre> list = dao.find(dao.query2Hql(OrgDeptPositionCentre.class, query), query);
            ArrayList<OrgPosition> arrayList = new ArrayList<>();
            list.forEach(orgDeptPositionCentre -> {
                arrayList.add(orgDeptPositionCentre.getOrgPosition());
            });
            orgPositionList = arrayList;
        }
        // tree树
        List<OrgPosition.Vo> vos = TreeBuilder.orgPositionBuildTree(new HashSet<>((List<OrgPosition.Vo>) VO.toVOCollection(orgPositionList)));
        PageHelper.startPage(orgPositionQuery.getPage(), orgPositionQuery.getSize());
        PageInfo pageInfo = new PageInfo(vos);
        pageInfo.setTotal(vos.size());
        return new Vo(pageInfo);
    }

    @ApiOperation("添加")
    @PostMapping("/save")
    @RequiresPermissions("sys:orgPosition:save")
    public Vo list(@RequestBody OrgPosition orgPosition) throws AdminException {

        List<OrgPosition> orgPositionBypostName = orgPositionRepository.findByDeletedAndPostName(false, orgPosition.getPostName());
        if(orgPositionBypostName != null && orgPositionBypostName.size() > 0) {
            throw new AdminException("此岗位已存在");
        }

        OrgPosition save = orgPositionService.save(orgPosition);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:orgPosition:update")
    public Vo update(@RequestBody OrgPosition orgPosition) throws AdminException {
        if(orgPosition.getId() == null) {
            throw new AdminException("缺参id");
        }

        OrgPosition orgPositionQuery = dao.get(OrgPosition.class, orgPosition.getId());
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }
        BeanUtils.copyProperties(orgPosition, orgPositionQuery, BeanNullUtil.getNullPropertyNames(orgPosition));
        orgPositionService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:orgPosition:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        OrgPosition orgPositionQuery = dao.get(OrgPosition.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        orgPositionService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

}
